﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ToDo.Data;
using ToDo.Model;
using System.Collections;

namespace ToDo.view.page
{
    /// <summary>
    /// ImportmentPage.xaml 的交互逻辑
    /// </summary>
    public partial class ImportmentPage : Page
    {
        private int count =0;
        private String SelectCommand = "SELECT * FROM IMPORTENT";
        private List<ImportentModel> list = new  List<ImportentModel>();
        public ImportmentPage()
        {
            InitializeComponent();
            show();
        }

        public void show()
        {
            using (SqlConnection connection = new SqlConnection(LinkSqlServer.source))
            {
                connection.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(SelectCommand, connection);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet, "IMPORTENT");
                foreach (DataRow row in dataSet.Tables["IMPORTENT"].Rows)
                {
                    count = (int)row[0];
                    if ((int)row[2] == 0)
                    {
                        list.Add(new ImportentModel() { ID = (int)row[0],Icon= "\xe61a", Message = (string)row[1], IsFinsih = (int)row[2] });
                    }
                    else
                    {
                        list.Add(new ImportentModel() { ID = (int)row[0],Icon= "\xe68b", Message = (string)row[1], IsFinsih = (int)row[2] });
                    }
                }
                LB_Importent.ItemsSource = list;


            }
            if (count == 0)
            {
                IV_background.Visibility = Visibility.Visible;
                LB_Importent.Visibility = Visibility.Collapsed;
            }
            else
            {
                IV_background.Visibility = Visibility.Collapsed;
                LB_Importent.Visibility = Visibility.Visible;
            }

           

        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                String sqlCommand = "INSERT INTO IMPORTENT VALUES("+ (count+1) +",'"+ TB_importent.Text.ToString() 
                    + "',"+0+",'"+DateTime.Now.ToString()+"')";
                LinkSqlServer.Add(sqlCommand);
                TB_importent.Text = "";
                list.Clear();
                LB_Importent.ItemsSource = null;
                show();
            }
        }



        private void TB_importent_MouseEnter(object sender, MouseEventArgs e)
        {
            TB_importent.Text = "";
        }

        private void TB_importent_MouseLeave(object sender, MouseEventArgs e)
        {
            TB_importent.Text = "在重要中添加任务";
        }

        private void LB_Importent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            String Deleate = "DELETE IMPORTENT WHERE ID = "+ list.ToArray()[LB_Importent.SelectedIndex].ID;
            LinkSqlServer.DoCommand(Deleate);
            list.Clear();
            LB_Importent.ItemsSource = null;
            show();
        }
    }
}
